// 应急电话路由
const express = require('express');
const router = express.Router();
const EmergencyPhone = require('../models/EmergencyPhone');

/**
 * 获取应急电话列表
 * @route GET /api/emergency/phones
 * @description 获取应急电话列表，支持分类筛选
 * @access Public
 */
router.get('/phones', async (req, res) => {
  try {
    const { category = '', country = '' } = req.query;
    
    const whereCondition = {
      status: 1 // 只返回正常状态的电话
    };
    
    // 分类筛选
    if (category) {
      whereCondition.category = category;
    }
    
    // 国家筛选
    if (country) {
      whereCondition.country = country;
    }
    
    // 查询应急电话
    const phones = await EmergencyPhone.findAll({
      where: whereCondition,
      order: [['order', 'ASC'], ['createdAt', 'DESC']]
    });
    
    res.status(200).json({ success: true, data: phones });
  } catch (error) {
    console.error('获取应急电话列表失败:', error);
    res.status(500).json({ message: '服务器错误', error: error.message });
  }
});

/**
 * 获取常用应急电话
 * @route GET /api/emergency/common-phones
 * @description 获取常用应急电话列表
 * @access Public
 */
router.get('/common-phones', async (req, res) => {
  try {
    // 查询常用应急电话
    const commonPhones = await EmergencyPhone.findAll({
      where: {
        isCommon: true,
        status: 1
      },
      order: [['order', 'ASC']]
    });
    
    res.status(200).json({ success: true, data: commonPhones });
  } catch (error) {
    console.error('获取常用应急电话失败:', error);
    res.status(500).json({ message: '服务器错误', error: error.message });
  }
});

/**
 * 获取电话分类
 * @route GET /api/emergency/categories
 * @description 获取所有电话分类
 * @access Public
 */
router.get('/categories', async (req, res) => {
  try {
    // 获取所有不重复的分类
    const categories = await EmergencyPhone.findAll({
      attributes: ['category'],
      group: ['category'],
      where: { status: 1 },
      order: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'DESC']]
    });
    
    // 格式化分类数据
    const formattedCategories = categories.map(item => item.category);
    
    res.status(200).json({ success: true, data: formattedCategories });
  } catch (error) {
    console.error('获取电话分类失败:', error);
    res.status(500).json({ message: '服务器错误', error: error.message });
  }
});

/**
 * 获取所有国家
 * @route GET /api/emergency/countries
 * @description 获取所有有应急电话的国家
 * @access Public
 */
router.get('/countries', async (req, res) => {
  try {
    // 获取所有不重复的国家
    const countries = await EmergencyPhone.findAll({
      attributes: ['country'],
      group: ['country'],
      where: {
        status: 1,
        country: {
          [Sequelize.Op.not]: null,
          [Sequelize.Op.ne]: ''
        }
      },
      order: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'DESC']]
    });
    
    // 格式化国家数据
    const formattedCountries = countries.map(item => item.country);
    
    res.status(200).json({ success: true, data: formattedCountries });
  } catch (error) {
    console.error('获取国家列表失败:', error);
    res.status(500).json({ message: '服务器错误', error: error.message });
  }
});

module.exports = router;